<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\BatchRestore;
use App\Admin\Actions\Grid\Restore;
use App\Admin\Repositories\Reply;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ReplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Reply(), function (Grid $grid) {

            $grid->showColumnSelector();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->showQuickEditButton();
            $grid->setDialogFormDimensions('1000px', '600px');

            $where = [];
            $topic_id = intval(request('topic_id'));
            if ($topic_id) {
                $where['topic_id'] = $topic_id;
            }
            $user_id = intval(request('user_id'));
            if ($user_id) {
                $where['user_id'] = $user_id;
            }
            $grid->model()->with(['topic', 'user'])->where($where);

            $grid->column('id')
                ->sortable()
                ->hide();

            $grid->column('topic_id')
                ->sortable()
                ->display(function () {

                    return '<a href="/admin/reply?topic_id=' . $this->topic_id . '" title="话题回复">' . $this->topic->title . '</a>';
                });

            $grid->column('user_id')
                ->sortable()
                ->display(function () {

                    return '<a href="/admin/reply?user_id=' . $this->user_id . '" title="用户回复">' . $this->user->name . '</a>';
                });

            $grid->column('content')
                ->display(function ($value) {
                    return $value;
                });

            $grid->column('created_at')
                ->sortable();

            $grid->column('updated_at')
                ->sortable()
                ->hide();


            // 回收站
            $grid->filter(function (Grid\Filter $filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();

                $filter->like('topic.title', '话题');
                $filter->like('user.name', '用户');
                $filter->like('content');
            });
            // 行恢复
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Reply::class));
                }
            });
            // 批量恢复
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Reply::class));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Reply(), function (Show $show) {
            $show->field('id');
            $show->field('topic_id');
            $show->field('user_id');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Reply(), function (Form $form) {


            $form->hidden('id');
            $form->editor('content', '')->width(12, 0);

        });
    }
}
